package com.vr.xuecheng.media.controller;

import com.vr.xuecheng.base.response.PageResult;
import com.vr.xuecheng.base.response.Result;
import com.vr.xuecheng.media.model.dto.MediaFilesPageQueryParamsDTO;
import com.vr.xuecheng.media.model.entity.MediaFiles;
import com.vr.xuecheng.media.model.vo.MediaFilesPageVO;
import com.vr.xuecheng.media.model.vo.UploadFileVO;
import com.vr.xuecheng.media.service.IMediaFilesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 媒资信息 前端控制器
 * </p>
 *
 * @author hzh
 */
@Slf4j
@RestController
@Tag(name = "媒资信息")
@RequiredArgsConstructor
public class MediaFilesController {

    private final IMediaFilesService mediaFilesService;

    /**
     * @param file 文件对象
     * @return {@code UploadFileVO } 结果
     * 上传文件
     */
    @Operation(summary = "上传文件")
    @PostMapping(value = "/upload/coursefile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public UploadFileVO upload(@RequestPart("filedata") MultipartFile file){
        return mediaFilesService.upload(file);
    }

    @PostMapping(value = "/upload/course-html-file", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result<String> upload(@RequestPart("file") MultipartFile file,@RequestParam("objectName") String objectName){
        System.out.println(file.getSize());
        return Result.toResult(mediaFilesService.upload(file, objectName));
    }
    /**
     * @param fileMd5 文件的md5值也是id
     * @return {@code Result<Boolean> } 返回是否存在
     * 1、检查文件是否存在
     */
    @Operation(summary = "检查文件是否存在")
    @PostMapping("/upload/checkfile")
    public Result<Boolean> checkFile(@RequestParam("fileMd5") String fileMd5) {
        return Result.ok(mediaFilesService.checkFile(fileMd5));
    }

    /**
     * @param fileMd5 分块前的文件的Md5值
     * @param chunkIndex 分块序号
     * @return {@code Result<Boolean> } 结果
     * 2、上传分块之前检查分块
     */
    @Operation(summary = "检查分块是否存在")
    @PostMapping("/upload/checkchunk")
    public Result<Boolean> checkChunk(@RequestParam("fileMd5") String fileMd5, @RequestParam("chunk") int chunkIndex) {
        return Result.ok(mediaFilesService.checkChunk(fileMd5, chunkIndex));
    }

    /**
     * @param file 分块文件
     * @param fileMd5 分块前文件的md5值
     * @param chunkIndex 分块序号
     * @return {@code Result<Boolean> } 结果
     * 3、检验通过，分块上传
     */
    @Operation(summary = "上传分块文件")
    @PostMapping( "/upload/uploadchunk")
    public Result<Boolean> uploadChunk(@RequestPart MultipartFile file,@RequestParam("fileMd5") String fileMd5, @RequestParam("chunk") int chunkIndex) {
        return mediaFilesService.uploadChunk(file, fileMd5, chunkIndex);
    }

    /**
     * @param fileMd5 分块前文件md5值
     * @param fileName 分块前文件名
     * @param chunkTotal 分块总数
     * @return {@code Result<Boolean> } 是否合并成功
     * 4、完成分块上传，开始合并分块
     */
    @PostMapping( "/upload/mergechunks")
    public Result<Boolean> mergeChunks(@RequestParam("fileMd5") String fileMd5, @RequestParam("fileName") String fileName, @RequestParam("chunkTotal") int chunkTotal) {
        return mediaFilesService.mergeChunks(fileMd5, fileName, chunkTotal);
    }


    /**
     * @param req 分页参数
     * @return {@code PageResult<MediaFilesPageVO> } 分页结果
     * 分页查询文件信息
     */
    @Operation(summary = "分页查询文件信息")
    @PostMapping("/files")
    public PageResult<MediaFilesPageVO> page(@RequestBody MediaFilesPageQueryParamsDTO req) {
        return mediaFilesService.page(req);
    }

    /**
     * @param fileId 文件id
     * @return {@code Result<String> } 结果
     * 图片预览
     */
    @Operation(summary = "图片预览")
    @GetMapping("/preview/{file-id}")
    public Result<String> preview(@PathVariable("file-id") String fileId) {
        return Result.ok(mediaFilesService.getById(fileId).getUrl());
    }
    @Operation(summary = "删除文件信息")
    @DeleteMapping("/{file-id}")
    public Result<String> delete(@PathVariable("file-id") String fileId) {
        return Result.toResult(mediaFilesService.deleteById(fileId));
    }
    @Operation(summary = "根据文件id获取文件url")
    @GetMapping("/get-info/{media-id}")
    public Result<String> getFilePathById(@PathVariable("media-id") String mediaId) {
        MediaFiles mediaFiles = mediaFilesService.lambdaQuery().eq(MediaFiles::getFileId, mediaId).one();
        return Result.ok(mediaFiles.getUrl());
    }

}
